package com.luozhengdong;


public class Game {
	private Qipan qipan;

	public Game() {
		this.qipan=new Qipan();
	}
	/**
	 *开始游戏
	 */
	public static void main(String[] args) {
		Game g = new Game();

		AbstractPlayer p1 = new HumanPlayer();
		p1.initName();
		p1.color = 1;// 黑子

		AbstractPlayer p2 = new AIPlayer();
		p2.name = "润发";
		p2.color = 2;// 白子
		g.start(p1, p2); 
	}
	void start(AbstractPlayer p1, AbstractPlayer p2) {
		// 1展示空棋盘
		qipan.draw();
		while (true) {
			// 2黑方下棋
			((HumanPlayer) p1).play(qipan);
			// 2.1黑方是否胜利了
			if(whoWin()==1) {
				System.out.println(p1.initName()+"赢了");
				break;
			}
			// 2.2棋盘是否满了，平局
			if (qipan.isFull()) {
				System.out.println("棋盘满了，平局");
				break;
			}
			// 3让白方下棋
			((AIPlayer) p2).play(qipan);
			// 3.1白方是否胜利了in
			if(whoWin()==2) {
				System.out.println(p2.initName()+"赢了");
				break;
			}
			// 3.2棋盘是否满了，平局
			if (qipan.isFull()) {
				System.out.println("棋盘满了，平局");
				break;
			}

		}
	}
	public int whoWin() {
		int l=0;
		int black=1;
		int white=1;
		for(int i=1;i<qipan.points.length;i++) {
			for(int j=1;j<qipan.points.length-1;j++) {
				if(black==6) {
					return 1;
				}
				if(white==6) {
					return 2;
				}
				
				if(qipan.points[i][j+1].color==1) {
					black++;
					white=1;
				}
				if(qipan.points[i][j-1].color==2) {
					black=1;
					white++;
				}
			}
			
		}
		return 0;
	}
}